package com.atguigu.controller;

import com.atguigu.client.ProductFeignClient;
import com.atguigu.client.SearchFeignClient;
import com.atguigu.result.RetVal;
import com.atguigu.search.SearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ShopIndexController {
    @Resource
    private ProductFeignClient productFeignClient;
    @Autowired
    private SearchFeignClient searchFeignClient;
    //让用户可在访问首页时  不带参数 或者index.html都可直接访问主页
    @RequestMapping({"/","/index.html"})
    public String index(Model model){
        RetVal categoryInfo = productFeignClient.getIndexCategoryInfo();
        model.addAttribute("list",categoryInfo.getData());
        return "index/index";
    }
    //商品的搜索
    @GetMapping("search.html")
    public String searchProduct(SearchParam searchParam,Model model){
        RetVal<Map> retVal = searchFeignClient.searchProduct(searchParam);
        model.addAllAttributes(retVal.getData());
        model.addAttribute("searchParam",searchParam);
        //1.设置搜索路径的参数
        String urlParam = getUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);
        //2.前端页面品牌的显示
        String brandName = getBrandName(searchParam.getBrandName());
        model.addAttribute("brandNameParam",brandName);
        //3.页面的平台属性的显示
        List<Map<String,String>> properList = getProperList(searchParam.getProps());
        model.addAttribute("propsParamList",properList);
        //4.页面的排序信息的显示
        String order = searchParam.getOrder();
        Map<String,Object> orderMap = getOrder(order);
        model.addAttribute("orderMap",orderMap);
        return "search/index";
    }

    //页面的排序信息的显示
    private Map<String,Object> getOrder(String order) {
        Map<String, Object> orderMap = new HashMap<>();
        if (!StringUtils.isEmpty(order)){
            String[] split = order.split(":");
            if (split != null && split.length==2){
                orderMap.put("type",split[0]);
                orderMap.put("sort",split[1]);
            }
        }else {
            //默认是热度 降序
            orderMap.put("type","1");
            orderMap.put("sort","desc");
        }
        return orderMap;
    }

    //前端页面品牌的显示
    private String getBrandName(String brandName) {
        if (!StringUtils.isEmpty(brandName)){
            String[] split = brandName.split(":");
            if (split != null && split.length == 2){
                return "品牌:"+split[1];
            }
        }
        return "";
    }

    //页面的平台属性的显示
    private List<Map<String, String>> getProperList(String[] props) {
        List<Map<String, String>> properList = new ArrayList<>();
        if (props != null && props.length>0 ){
            for (String prop : props) {
                String[] split = prop.split(":");
                if (split != null && split.length==3){
                    Map<String, String> map = new HashMap<>();
                    map.put("propertyKeyId",split[0]);
                    map.put("propertyValue",split[1]);
                    map.put("propertyKey",split[2]);
                    properList.add(map);
                }
            }
        }
        return properList;
    }

    //设置搜索路径的参数
    private String getUrlParam(SearchParam searchParam) {
        StringBuilder urlParam = new StringBuilder();
        //keyword 后面拼接的搜索名称
        if (!StringUtils.isEmpty(searchParam.getKeyword())){
            urlParam.append("keyword=").append(searchParam.getKeyword());
        }
        //根据分类id搜索
        if (searchParam.getCategory1Id() != null){
            urlParam.append("category1Id=").append(searchParam.getCategory1Id());
        }
        if (searchParam.getCategory2Id() != null){
            urlParam.append("category2Id=").append(searchParam.getCategory2Id());
        }
        if (searchParam.getCategory3Id() != null){
            urlParam.append("category3Id=").append(searchParam.getCategory3Id());
        }
        //根据品牌搜索
        if (!StringUtils.isEmpty(searchParam.getBrandName())){
            //因为有之前的搜索才能出现 品牌logo 点logo显示搜索
            if (urlParam.length() > 0){
                urlParam.append("&brandName=").append(searchParam.getBrandName());
            }
        }
        //加上平台属性的搜索
        String[] properProps = searchParam.getProps();
        if (properProps != null && properProps.length>0){
            for (String properProp : properProps) {
                if (urlParam.length() > 0){
                    urlParam.append("&props=").append(properProp);
                }
            }
        }
        return "search.html?"+urlParam.toString();
    }
}
